package ilearn.seata.job.java8.account.service;

import ilearn.seata.job.java8.account.dao.PointDao;
import ilearn.seata.job.java8.account.dao.PointHistoryDao;
import ilearn.seata.job.java8.account.entity.Points;
import ilearn.seata.job.java8.account.entity.PointsHistory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PointsService {

    @Resource
    private PointDao pointDao;
    @Resource
    private PointHistoryDao pointHistoryDao;

    public Points increse(String accId, Integer change){
        Points point = pointDao.findByAccId(accId);
        if(point == null){
            point = Points.builder().accId(accId).points(0).build();
            pointDao.insert(point);
        }
        point.setPoints(point.getPoints() + change);
        pointDao.update(point);
        PointsHistory pointsHistory = PointsHistory.builder().accId(accId).points(point.getPoints()).changes(change)
                .build();
        pointHistoryDao.insert(pointsHistory);
        return point;
    }
}
